# -*- coding:utf-8 -*-
# author:Li Wang
# @Time : 2022/4/14 16:06
import pandas as pd
import glob


#机油粘度对载重的影响

def calculate_fuel(df):
    pass

if __name__ == '__main__':
    file_source_path = r'F:\wangli\ruixiude_code\daolu_working_condition_identification\datasets\苏BL6198 4.22-28数据源_results\working_condition\*.csv'
    filepaths = glob.glob(file_source_path)
    filepaths.sort()
    overall_df = []
    for each_file_path in filepaths:
        print(each_file_path)
        df = pd.read_csv(each_file_path)
        df['fuel'] = df['Epm_nEng']/60 * 6 / 2 * df['InjCtl_qCurr'] / 1000000 / 0.845 / 1
        df['mileage'] = df['VehV_v'] / 3600
        # df['fuel_100km'] = np.nan
        df_new = pd.DataFrame()
        # temp_path_list = each_file_path.split('\\')[-1].split('_')
        vehicle_number = each_file_path.split('\\')[-1].split(' ')[0]
        date = each_file_path.split('\\')[-1].split(' ')[1:][0].split('_')[0].rstrip('数据')
        load_condition = each_file_path.split('\\')[-1].split(' ')[1:][0].split('_')[1]

        for key,value in df.groupby(by='working_condition'):
            value['total_fuel'] = value['fuel'].sum()
            value['total_mileage'] = value['mileage'].sum()
            if key == '正常行驶':
                fuel_c = value['fuel'].sum() / value['mileage'].sum() * 100#百公里油耗
            if key == '车辆驻车，发动机怠速' or key == '装料，卸料，罐体清洗':
                fuel_c = value['fuel'].sum() / len(value) * 3600#小时油耗
            df_new = pd.DataFrame({
                '车号':[vehicle_number],
                '日期':[date],
                '载荷状态':[load_condition],
                '时长':[len(value)],
                '油耗':[fuel_c],
                '平均车速':[value['VehV_v'].mean()],
                '工况':[key]
            })
            # df_new['车号'] = temp_path_list[0] + '_' + temp_path_list[1]
            # df_new['日期'] = temp_path_list[2]
            # df_new['载荷状态'] = temp_path_list[3]
            # df_new['时长'] = len(value)
            # df_new['油耗'] = fuel_c
            # df_new['平均车速'] = value['VehV_v'].mean()
            overall_df.append(df_new)
        # break
    pd.concat(overall_df).to_csv(r'F:\wangli\ruixiude_code\daolu_working_condition_identification\datasets\苏BL6198 4.22-28数据源_results\fuel_statistic\working_results.csv',index=False,encoding='utf_8_sig')
